package runnables;


public class AnimationLoop<Anim extends Animated> extends Thread{
	private Anim container;
	private boolean animationRunning = false;
	private int delay;
	private Object animationLock = new Object();
		
	public AnimationLoop(Anim containerPanel, int delayTime){
		container = containerPanel;
		delay = delayTime;
	}
	
	public void run(){
		if(animationRunning){
			return;
		}
		animationRunning = true;
		synchronized(animationLock){
			while(animationRunning){
				container.repaintBody();
				container.advanceFrame();
				try {
					animationLock.wait(delay);
				}
				catch (InterruptedException e) {
					System.out.println("Animation error: "+e);
					return;
				}			
			}
		}
	}
	
	public void stopThread(){
		animationRunning = false;
		try {
			join();
		}
		catch (InterruptedException e) {
			System.out.println("Thread Error: "+e);
			System.exit(1);
		}
	}

}
